import Vue from 'vue'
import Vuex from 'vuex'

import app from './common/app'
import user from './common/user'

Vue.use(Vuex)

export default new Vuex.Store({
  namespaced: false,
  state: {
    // 已加载模块
    loadedModules: []
    //
  },
  getters: {
    loadedModules: (state) => {
      return state.loadedModules
    }
  },
  mutations: {
    // 添加加载过的Store模块
    addLoadedModule (state, moduleName) {
      state.loadedModules.push(moduleName)
    }
  },
  actions: {
    // 添已加载模块
    addLoadedModule ({ commit, state }, moduleName) {
      commit('addLoadedModule', moduleName)
    },
    // 查询已加载模块
    findModule ({ commit, state }, module) {
      // console.log('findModule module', module)
      if (state.loadedModules.indexOf(module.name) > -1) {
        return {name: module.name, loaded: true, storeModule: module.storeModule()}
      } else {
        return {name: module.name, loaded: false, storeModule: module.storeModule()}
      }
    }
  },
  modules: {
    app,
    user
  }
})
